package work.parabola.assets.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateTimeUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(DateTimeUtil.class);

	public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static final String DATE_FORMAT = "yyyy-MM-dd";

	public static String dateToStr(Date date) {
		return DateTimeUtil.dateToStr(date, DateTimeUtil.STANDARD_FORMAT);
	}

	public static String dateOnlyToStr(Date date) {
		return DateTimeUtil.dateToStr(date, DateTimeUtil.DATE_FORMAT);
	}

	public static String dateToStr(Date date, String format) {
		if(date == null){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static Date strToDate(String str, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = formatter.parse(str);
		} catch (ParseException e) {
			logger.error("日期格式错误");
		}
		return date;
	}
	public static Date strToDate(String str){
		return DateTimeUtil.strToDate(str, DateTimeUtil.STANDARD_FORMAT);
	}
	public static Date strToDateOnly(String str){
		return DateTimeUtil.strToDate(str, DateTimeUtil.DATE_FORMAT);
	}
	
	public static long diffDays(Date current, Date early){
		long currentTime = current.getTime();
		long earlyTime = early.getTime();
		
		long diff = currentTime - earlyTime;
		if(diff < 0){
			return 0;
		}
		return diff / (1000 * 60 * 60 * 24);
	}
	
	public static Date splitOnlyDay(Date date) {
		String onlyDay = DateTimeUtil.dateOnlyToStr(date);
		return DateTimeUtil.strToDateOnly(onlyDay);
	}
	
	public static Date today() {
		return DateTimeUtil.splitOnlyDay(new Date());
	}
	
	public static boolean isCurrentDay(Date date) {
		date = DateTimeUtil.splitOnlyDay(date);
		Date today = DateTimeUtil.today();
		return date.getTime() == today.getTime();
	}
	
	public static boolean isLaterDays(Date date) {
		date = DateTimeUtil.splitOnlyDay(date);
		Date today = DateTimeUtil.today();
		return date.getTime() > today.getTime();
	}
	
	public static boolean isEarlierDays(Date date) {
		date = DateTimeUtil.splitOnlyDay(date);
		Date today = DateTimeUtil.today();
		return date.getTime() < today.getTime();
	}
	
	public static boolean isLateOrCurrentDays(Date date) {
		return !DateTimeUtil.isEarlierDays(date);
	}
	
	public static boolean isEarlierOrCurrentDays(Date date) {
		return !DateTimeUtil.isLaterDays(date);
	}
}



























